//
//  BPWiFiUploadManager.m
//  BPTV
//
//  Created by baipeng on 2018/11/16.
//  Copyright © 2018年 Apple Inc. All rights reserved.
//

#import "BPWiFiUploadManager.h"
#import "BPHTTPConnection.h"
#import "BPWiFiViewController.h"

@interface BPWiFiUploadManager ()
@property (nonatomic, copy) BPWiFiUploadManagerFileUploadStartBlock startBlock;
@property (nonatomic, copy) BPWiFiUploadManagerFileUploadProgressBlock progressBlock;
@property (nonatomic, copy) BPWiFiUploadManagerFileUploadFinishBlock finishBlock;

@end

@implementation BPWiFiUploadManager{
    NSString *_tmpFileName;
    NSString *_tmpFilePath;
}

+(instancetype)shareInstance{
    static BPWiFiUploadManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [self new];
    });
    return manager;
}
- (instancetype)init{
    self = [super init];
    if (self) {
        self.webPath = [[NSBundle mainBundle] resourcePath];
        self.savePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    }
    return self;
}
#pragma mark - public method -
//TODO:开启代理
-(BOOL)startHTTPServerAtPort:(UInt16)port{
    HTTPServer *server = [HTTPServer new];
    server.port = port;
    self.httpServer = server;
    [self.httpServer setDocumentRoot:self.webPath];
    [self.httpServer setConnectionClass:[BPHTTPConnection class]];
    NSError *error = nil;
    [self.httpServer start:&error];
    if (error == nil) {
        [self setupStart];
    }
    return error == nil;
}
//TODO:关闭服务
- (void)stopHTTPServer {
    [self.httpServer stop];
    [self setupStop];
}
//TODO:是否开启工作
- (BOOL)isServerRunning {
    return self.httpServer.isRunning;
}
//TODO:获取ip
-(NSString *)ip{
    return [BPIPHelper deviceIPAdress];
}
//TODO:获取端口
-(UInt16)port{
    return self.httpServer.port;
}
//TODO:开始服务
- (BOOL)startHTTPServerAtPort:(UInt16)port
                        start:(BPWiFiUploadManagerFileUploadStartBlock)start
                     progress:(BPWiFiUploadManagerFileUploadProgressBlock)progress
                       finish:(BPWiFiUploadManagerFileUploadFinishBlock)finish{
    self.startBlock = start;
    self.progressBlock = progress;
    self.finishBlock = finish;
    return [self startHTTPServerAtPort:port];
}
//TODO:业务交互
- (void)showWiFiPageFrontViewController:(UIViewController *)viewController dismiss:(void (^)(void))dismiss{
    BPWiFiViewController *vc = [[BPWiFiViewController alloc]init];
    vc.dismissBlock = dismiss;
    [viewController presentViewController:[[BaseNavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
}


#pragma mark - private method -
//TODO:开启连接监听
-(void)setupStart{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileUploadStart:) name:BPFileUploadDidStartNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileUploadFinish:) name:BPFileUploadDidEndNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileUploadProgress:) name:BPFileUploadProgressNotification object:nil];
}
//TODO:关闭监听
-(void)setupStop{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.startBlock = nil;
    self.progressBlock = nil;
    self.finishBlock = nil;
}

#pragma mark - Notification Callback
//TODO:开始上传
- (void)fileUploadStart:(NSNotification *)nof {
    NSString *fileName = nof.object[@"fileName"];
    NSString *filePath = [self.savePath stringByAppendingPathComponent:fileName];
    _tmpFileName = fileName;
    _tmpFilePath = filePath;
    if (self.startBlock) {
        self.startBlock(fileName, filePath);
    }
}
//TODO:上传结束
- (void)fileUploadFinish:(NSNotification *)nof {
    if (self.finishBlock) {
        self.finishBlock(_tmpFileName, _tmpFilePath);
    }
}
//TODO:上传进度
- (void)fileUploadProgress:(NSNotification *)nof {
    CGFloat progress = [nof.object[@"progress"] doubleValue];
    NSLog(@"%f",progress);
    if (self.progressBlock) {
        self.progressBlock(_tmpFileName, _tmpFilePath, progress);
    }
}

#pragma mark - Block Setter
- (void)setFileUploadStartCallback:(BPWiFiUploadManagerFileUploadStartBlock)callback {
    self.startBlock = callback;
}
- (void)setFileUploadProgressCallback:(BPWiFiUploadManagerFileUploadProgressBlock)callback {
    self.progressBlock = callback;
}
- (void)setFileUploadFinishCallback:(BPWiFiUploadManagerFileUploadFinishBlock)callback {
    self.finishBlock = callback;
}

- (void)dealloc {
    [self setupStop];
}




@end
